package com.jbp.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jbp.config.GlobConfig;
import com.jbp.dao.WalletMentionDao;
import com.jbp.exception.RRException;
import com.jbp.model.*;
import com.jbp.service.*;
import com.jbp.utils.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.jbp.utils.PageUtils;
import com.jbp.utils.Query;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * 提现(WalletMention)表服务实现类
 * @author cc
 * @since 2020-07-02 11:45:11
 */
@Service
public class WalletMentionServiceImpl extends ServiceImpl<WalletMentionDao, WalletMention> implements WalletMentionService {
    @Autowired
    private WalletService walletService;
    @Autowired
    private WalletRecordService walletRecordService;
    @Autowired
    private IdentifyService identifyService;

    @Autowired
    private RankProportionService rankProportionService;


    @Autowired
    private ReciverAddressService reciverAddressService;
    @Autowired
    private PropertyService propertyService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<WalletMention> queryWrapper = new QueryWrapper<>();
        if (params.get("userId") != null){
            queryWrapper.eq("user_id",params.get("userId"));
        }
        queryWrapper.orderByDesc("create_time");
        IPage<WalletMention> page = this.page(
                new Query<WalletMention>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    @Transactional
    public synchronized void mention(WalletMention walletMention) {
        //是否实名认证
        Identify identify = identifyService.getOne(new QueryWrapper<Identify>().eq("user_id", walletMention.getUserId()));
        if (identify == null) {
            throw new RRException("请先实名认证");
        }

        //是否添加收款地址
        ReciverAddress reciverAddress = reciverAddressService.getOne(new QueryWrapper<ReciverAddress>().eq("user_id", walletMention.getUserId()));
        if (reciverAddress == null) {
            throw new RRException("请先绑定支付宝");
        }

        //提现记录
        Wallet wallet = walletService.getByUserId(walletMention.getUserId());
        BigDecimal aixin = walletMention.getAixin().multiply(BigDecimal.valueOf(GlobConfig.moneyTimes));
        if (wallet.getAixin().compareTo(aixin)<0) {
            throw new RRException("爱心余额不足");
        }

        //提现手续费
        String srate = propertyService.getV("mention_rate");
        double rate = 0d;
        if (srate!=null) {
            rate = Double.parseDouble(srate);
        }
        BigDecimal money = aixin.divide(BigDecimal.valueOf(GlobConfig.moneyTimes)).multiply(BigDecimal.valueOf(1d-rate));
        walletMention.setMoney(money);
        walletMention.setAixin(aixin);
        walletMention.setStatus("create");
        walletMention.setOrderNo(createOrderNo());
        save(walletMention);

        //余额变动记录
        WalletRecord walletRecord = new WalletRecord();
        walletRecord.setUserId(walletMention.getUserId());
        walletRecord.setCoinType("aixin");
        walletRecord.setAddType("sub");
        walletRecord.setOperationType("mention");
        walletRecord.setBeforeBalance(wallet.getAixin());
        walletRecord.setChangeBalance(aixin);
        walletRecord.setAfterBalance(wallet.getAixin().subtract(aixin));
        walletRecordService.save(walletRecord);

        //余额变动
        wallet.setAixin(wallet.getAixin().subtract(aixin));
        walletService.updateWithLock(wallet);
    }

    /**生成订单号*/
    private String createOrderNo() {
        String time = new SimpleDateFormat("MMddHHmmss").format(new Date());
        String orderNo = time+ UUIDUtil.getNumber(4);
        WalletMention order = getByOrderNo(orderNo);
        return order==null ? orderNo : createOrderNo();
    }

    WalletMention getByOrderNo(String orderNo) {
        QueryWrapper<WalletMention> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_no", orderNo);
        return getOne(queryWrapper);
    }
}
